package com.weai.controller;

import com.weai.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import com.weai.dto.ApiResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/reports")
public class ReportController {

    @Autowired
    private ReportService reportService;

    @PostMapping("/upload")
    public ApiResponse<String> uploadReport(@RequestParam("file") MultipartFile file) {
        try {
            reportService.saveReportFile(file);
            log.info("上传成功: {}", file.getOriginalFilename());
            return ApiResponse.success("上传成功");
        } catch (Exception e) {
            log.error("上传失败: {}", file.getOriginalFilename(), e);
            return ApiResponse.fail(400,"上传失败: ");
        }
    }

    @GetMapping("/list")
    public ApiResponse<List<String>> listReports() {
        try {
            List<String> files = reportService.listReportFiles();
            log.info("获取报告列表成功, 共{}个文件", files.size());
            return ApiResponse.success(files);
        } catch (Exception e) {
            log.error("获取报告列表失败", e);
            return ApiResponse.fail(400,"获取失败: ");
        }
    }

    @GetMapping("/content")
    public ApiResponse<String> getReportContent(@RequestParam("filename") String filename) {
        try {
            String content = reportService.getReportFileContent(filename);
            log.info("读取报告内容成功: {}", filename);
            return ApiResponse.success("读取成功", content);
        } catch (Exception e) {
            log.error("读取报告内容失败: {}", filename, e);
            return ApiResponse.fail(400,"读取失败: ");
        }
    }
} 